package com.tcwlkj.uuService.ui.set;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.UserCardModel;
import com.tcwlkj.uuService.ui.BaseActivity;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.tcwlkj.uuService.utils.Tools;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/18 0018.
 */

public class BankActivity extends BaseActivity {

    @Bind(R.id.ll_add_bankcard)
    LinearLayout llAddBankcard;
    @Bind(R.id.activity_bank_card)
    RelativeLayout activityBankCard;
    @Bind(R.id.tv_bank_number)
    TextView tv_bank_number;
    @Bind(R.id.tv_base_right)
    TextView tv_base_right;
    private CommonResponse<UserCardModel> commonResponse;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_card);
        ButterKnife.bind(this);
        userCard();
    }

    private void userCard() {
        Call<CommonResponse<UserCardModel>> httpResultCall = RetrofitUtils.getRequest(this, true).usercard(Integer.parseInt(PreferencesUtils.getString(MyApp.getContext(),"userID")),1);
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<UserCardModel>>() {
            @Override
            public void onSuccess(Object t) {
                commonResponse = (CommonResponse<UserCardModel>) t;
                //userCardModel = (UserCardModel) t;
                if(commonResponse.getData() ==null){//没有银行卡
                    changeTitle("绑定银行卡");
                    llAddBankcard.setVisibility(View.VISIBLE);
                    activityBankCard.setVisibility(View.GONE);
                }else {//有银行卡
                    changeTitle("银行卡");
                    llAddBankcard.setVisibility(View.GONE);
                    activityBankCard.setVisibility(View.VISIBLE);
                    Typeface mtypeface=Typeface.createFromAsset(getAssets(),"BankText.ttf");
                    tv_bank_number.setTypeface(mtypeface);
                    String str= commonResponse.getData().getNumber();
                    tv_bank_number.setText(str.substring(str.length()-4,str.length()));
                    tv_base_right.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void onFail(int code, String message) {
                if(code == 40104){
                    userCard();
                }else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }

    @OnClick({R.id.ll_add_bankcard,R.id.tv_base_right})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.ll_add_bankcard:
                Intent intent = new Intent(baseContext,BindBankCard.class);
                startActivityForResult(intent,1);
                break;
            case R.id.tv_base_right:
                Dialogshow();
                break;
        }
    }

    private void Dialogshow() {
        final String[] stringItems = {"换绑"};
        final ActionSheetDialog dialog = new ActionSheetDialog(baseContext, stringItems, null);
        dialog.itemTextColor(Color.parseColor("#000000"))
                .itemTextSize(14.5f)
                .cancelText(Color.parseColor("#000000"))
                .cancelTextSize(14.5f)
                .show();
        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0: // 换绑
                        Intent intent = new Intent(baseContext,BindBankCard.class);
                        intent.putExtra("name",commonResponse.getData().getName());
                        intent.putExtra("number",commonResponse.getData().getNumber());
                        intent.putExtra("adress",commonResponse.getData().getBankAddress());
                        startActivityForResult(intent,1);
                        break;
                }
                dialog.dismiss();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==2){
            userCard();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
